<?php
/* 
 * ********* CVS HEADER ********* 
 * 
 * $RCSfile: sauvegarde.php,v $ 
 * $Date: 2005/08/31 10:13:46 $ 
 * $Revision: 1.2 $ 
 * 
 * ********* CVS HEADER ********* 
 */
function creer_repertoire($path,$droit){
	// on recupere le path du parent
	//echo "path : $path<br />";
	$parent = parent_repertoire($path);
	if($parent=='./')$parent='';
	if($parent=='/')$parent='';
	if(!repertoire_exists($parent)){
		creer_repertoire($parent,$droit);	
	}
	$id_parent = id_repertoire($parent);
	$nom = nom_repertoire($path);
	//echo "parent $id_parent: $parent<br />";
	$query ="INSERT INTO html_repertoire (nom,path,droit,id_parent)";
	$query.=" VALUES ('$nom','$path','$droit','$id_parent')";
	//echo $query.'<br/>';
	@rportalrequete($query) or rportalerreur("$query","?op=");
}

function nom_repertoire($path){
	if($path[strlen($path)-1]=='/'){
		return basename(substr($path, 0, -1));	
	}else{
		return basename($path);
	}
}

function parent_repertoire($path){
	if($path[strlen($path)-1]=='/'){
		return dirname(substr($path, 0, -1)).'/';	
	}else{
		return dirname($path).'/';
	}
}

function id_repertoire($path){
	$query = "SELECT id FROM html_repertoire WHERE path='$path'";
	$result=rportalrequete($query) or rportalerreur("$query","?op=");
	if(rportalnbresult($result)==0){
		return false;		
	}
	$row = rportalligne($result);
	return $row['id'];
}

function repertoire_exists($path){
	$query = "SELECT id FROM html_repertoire WHERE path='$path'";
	$result=rportalrequete($query) or rportalerreur("$query","?op=");
	if(rportalnbresult($result)==0){
		return false;		
	}
	return true;
}

function creer_page($path,$type,$titre,$description,$auteur,$fichier,$script,$droit){
	$nom = basename($path);
	// on recupere le type id
	$query = "SELECT id FROM html_type WHERE nom='$type'";
	$result=rportalrequete($query) or rportalerreur("$query","?op=");
	$type_id=1;
	if($row = rportalligne($result)){
		$type_id=$row['id'];		
	}
	// on recupere le repertoire id
	$rep = dirname($path).'/';
	if($rep=='./') $rep='';
	if($rep=='/') $rep='';
	$repertoire = id_repertoire($rep);
	if(page_exists($path)){
		$query = "UPDATE html SET nom='".addslashes($nom)."',titre='".addslashes($titre)."',description='".addslashes($description)."',modification=NOW(),id_repertoire='$repertoire',type='$type_id',login_auteur='$auteur',publier='0' WHERE code='$path'";
	}else{
		$query="INSERT INTO html (nom,titre,code,description,id_menu,droit,modification,id_repertoire,affiche_compteur,verrou,affiche_modification,inenv,type,login_auteur) VALUES ('".addslashes($nom)."','".addslashes($titre)."','$path','".addslashes($description)."','0','$droit',NOW(),'$repertoire','0','0','0','1','$type_id','$auteur')";
	}
	$result=rportalrequete($query) or rportalerreur("$query","?op=");
	$page = id_page($path);
	$new_dest=sprintf("./html/%07d.php", $page);
	$fp = fopen($new_dest, "w");
	fwrite($fp, $fichier);
	fclose($fp);
	if($type_id!=1 && $script!=''){
		eval($script);
	}
}

function page_exists($path){
	$query="SELECT id FROM html WHERE code='$path'";	
	$result=rportalrequete($query) or rportalerreur("$query","?op=");
	if(rportalnbresult($result)==0){
		return false;		
	}
	return true;
}

function id_page($path){
	$query="SELECT id FROM html WHERE code='$path'";	
	$result=rportalrequete($query) or rportalerreur("$query","?op=");
	if(rportalnbresult($result)==0){
		return false;		
	}
	$row = rportalligne($result);
	return $row['id'];
}
?>